#include "stackview.hpp"
#include "ui_stackview.h"

StackView::StackView(QWidget *parent) :
    QDockWidget(parent),
    ui(new Ui::StackView)
{
    ui->setupUi(this);
}


StackView::~StackView()
{
    while (!this->stackItems.isEmpty())
	delete this->stackItems.pop();

    delete ui;
}


void StackView::reset()
{
    while (!this->stackItems.isEmpty())
	delete this->stackItems.pop();
}

void StackView::changeEvent(QEvent *e)
{
    QDockWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}


void StackView::onPushStack(int adress, int level)
{
    //Generate the new Stack Item
    QString text = QString::number(adress, 16);
    while (text.size() < 4)
	text.prepend("0");

    QTableWidgetItem *newStackItem = new QTableWidgetItem(text.toUpper());

    if (this->stackItems.size() == 8)
    {
	delete this->stackItems.first();
	this->stackItems.pop_front();
    }

    this->stackItems.push(newStackItem);
    this->updateView();
}


void StackView::onPopStack()
{
    delete this->stackItems.pop();
    this->updateView();
}


void StackView::updateView() const
{
    for (int i = 0; i < ui->tbl_stack->rowCount(); i++)
	ui->tbl_stack->takeItem(i, 0);

    for (int i = 0; i < this->stackItems.size(); i++)
	ui->tbl_stack->setItem(this->stackItems.size() - 1 - i, 0, this->stackItems.at(i));
}


void StackView::onStackOverflow()
{

}
